package com.jmyu.meetappointment.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;







import java.util.Locale;


import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.GetListener;
import cn.bmob.v3.listener.UpdateListener;

import com.jmyu.meetappointment.R;
import com.jmyu.meetappointment.bean.Meet;
import com.jmyu.meetappointment.bean.MyUser;
import com.jmyu.meetappointment.bean.Time;
import com.jmyu.meetappointment.util.DataUtil;
import com.jmyu.meetappointment.util.MyProgressDialog;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class MeetAdapter extends BaseAdapter {
	private final static String TAG = "MeetAdapterTAG";
	private Context context;
	MyProgressDialog mProgressDialog;
	DataUtil dataUtil;
	String start_time = null;		
	String end_time = null;
	private List<Meet> list=new ArrayList<Meet>();
	String currentDay;
	MyUser currentUser;
	public MeetAdapter(Context context){
		this.context=context;
		mProgressDialog = new MyProgressDialog(context);
		currentUser = BmobUser.getCurrentUser(context, MyUser.class);
	}
	public void setData(List<Meet> list){
		this.list=list;
		notifyDataSetChanged();
	}
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int mPosition = position; 
		convertView = LayoutInflater.from(context).inflate(R.layout.item,null);
		TextView tv_meet_day = (TextView) convertView.findViewById(R.id.tv_meet_day);
		final TextView tv_meet_time = (TextView) convertView.findViewById(R.id.tv_meet_time);
		TextView tv_meet_name = (TextView) convertView.findViewById(R.id.tv_meet_name);
		TextView tv_meet_state = (TextView) convertView.findViewById(R.id.tv_meet_state);
		final TextView tv_meet_username = (TextView) convertView.findViewById(R.id.tv_meet_username);
		final Button yuding_button = (Button) convertView.findViewById(R.id.yuding_button);
		String state_id = list.get(position).getState_id();
		yuding_button.setText(state_id.equals("1")?"预定":"退订");
		String value=yuding_button.getText().toString().trim();
		yuding_button.setTextColor(value.equals("预定")?Color.GREEN:Color.RED);
		currentDay = list.get(position).getDay();
		final String meet_day = list.get(position).getDay().replace("-", "");
		tv_meet_day.setText(list.get(position).getDay());
		String time_id = list.get(position).getTime_id();				
		if(time_id!=null && !"".equals(time_id)){
			BmobQuery<Time> query = new BmobQuery<Time>();
			query.addWhereEqualTo("time_id", time_id);
			query.findObjects(context, new FindListener<Time>() {

				@Override
				public void onSuccess(List<Time> list) {
					Time time = list.get(0);
					start_time = time.getTime_start();
					end_time = time.getTime_end();
					tv_meet_time.setText("时间："+start_time+"-"+end_time);
					//判断日期是否过期
					SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
					StringBuffer meet_end_time = new StringBuffer(meet_day).append(end_time.replace(":","")).append("00");
					try {
						long isOutTime = sdf.parse(meet_end_time.toString()).getTime();
						long nowTime = System.currentTimeMillis();
						if(nowTime >= isOutTime){
							yuding_button.setText("已过期");
							yuding_button.setTextColor(Color.GRAY);
							yuding_button.setEnabled(false);
						}
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}

				@Override
				public void onError(int arg0, String arg1) {
					Log.d(TAG, "error : "+arg1);					
				}
			});
		}		

		tv_meet_name.setText(list.get(position).getMeet_name()+"会议室");
		tv_meet_state.setText("预定状态："+((list.get(position).getState_id().equals("1"))?"未预定":"已预订"));
		String user_id = list.get(position).getUser_id();				
		if(user_id!=null && !"".equals(user_id)){
			BmobQuery<MyUser> query = new BmobQuery<MyUser>();
			query.getObject(context, user_id, new GetListener<MyUser>() {
				@Override
				public void onSuccess(MyUser myuser) {
					if(list.get(mPosition).getState_id().equals("1")){
						tv_meet_username.setText("预定人：暂无");
					}else{
						tv_meet_username.setText("预定人："+myuser.getUsername());
					}	
				}

				@Override
				public void onFailure(int arg0, String msg) {
					Log.d(TAG, "获取当前用户失败："+msg);
				}
			});
		}else{
			tv_meet_username.setText("预定人：暂无");
		}
		//判断是否是当前用户
		final String currentUsername = currentUser.getUsername();
		String SubscribedUserId =  list.get(position).getUser_id();
		BmobQuery<MyUser> query = new BmobQuery<MyUser>();
		query.getObject(context, SubscribedUserId, new GetListener<MyUser>() {
			
			@Override
			public void onSuccess(MyUser user) {
				Log.d(TAG,"查询已预订人username成功");
				String SubscribedUserName = user.getUsername();
				if(SubscribedUserName != null && !"".equals(SubscribedUserName)){
					if(!SubscribedUserName.equals(currentUsername)&&yuding_button.getText().equals("退订")){
						yuding_button.setEnabled(false);
						yuding_button.setTextColor(Color.GRAY);
					}
				}
			}
			
			@Override
			public void onFailure(int arg0, String msg) {
				Log.d(TAG,"查询已预订人username失败:"+msg);
			}
		});
		
			

		yuding_button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Button button = (Button) v;
				String meetId = list.get(mPosition).getObjectId();
				String flag = button.getText().toString().trim();
				mProgressDialog.show();
				if(flag.equals("预定")){
					Meet meet = new Meet();
					meet.setState_id(0+"");
					meet.setUser_id(currentUser.getObjectId());
					meet.update(context, meetId, new UpdateListener() {

						@Override
						public void onSuccess() {
							Log.d(TAG, "预订会议室成功");
							BmobQuery<Meet> query = new BmobQuery<Meet>();
							query.addWhereEqualTo("day", currentDay);
							query.findObjects(context, new FindListener<Meet>() {

								@Override
								public void onSuccess(List<Meet> list) {
									setData(list);
									mProgressDialog.dismiss();
								}

								@Override
								public void onError(int arg0, String arg1) {
									Log.d(TAG, "刷新列表失败：" + arg1);
								}
							});
						}

						@Override
						public void onFailure(int arg0, String arg1) {
							Log.d(TAG, "预订会议室失败：" + arg1);

						}
					});
				}else if(flag.equals("退订")){
					Meet meet = new Meet();
					meet.setState_id(1+"");
					meet.setUser_id(null);
					meet.update(context, meetId, new UpdateListener() {

						@Override
						public void onSuccess() {
							Log.d(TAG, "退订会议室成功");
							BmobQuery<Meet> query = new BmobQuery<Meet>();
							query.addWhereEqualTo("day", currentDay);
							query.findObjects(context, new FindListener<Meet>() {

								@Override
								public void onSuccess(List<Meet> list) {
									setData(list);
									mProgressDialog.dismiss();
								}

								@Override
								public void onError(int arg0, String arg1) {
									Log.d(TAG, "刷新列表失败：" + arg1);
								}
							});
						}

						@Override
						public void onFailure(int arg0, String arg1) {
							Log.d(TAG, "退订会议室失败：" + arg1);
						}
					});


				}

			}
		});

		return convertView;
	}



}
